package com.scedu.controller;

import com.scedu.domain.Girl;
import com.scedu.repository.GirlRepository;
import com.scedu.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class GirlController {
    @Autowired
    GirlService girlService;
    @Autowired
    GirlRepository girlRepository;

    /*
    获取列表
     */
    @GetMapping("/girls")
    public List<Girl> getgirls(){
        return girlRepository.findAll();
    }
    /*
    创建女郎
     */
    @PostMapping("/add")
   // public Girl add(@RequestParam(value = "age") Integer age,
    //                @RequestParam(value = "cupsize") String cupsize){
      //  Girl girl=new Girl();
     //   girl.setAge(age);
    //    girl.setCupSize(cupsize);
    //    return girlRepository.save(girl);
   // }
     public Girl add(@Valid Girl girl, BindingResult bindingresult){
        if (bindingresult.hasErrors()){
            //有验证错误
            System.out.println(bindingresult.getFieldError().getDefaultMessage());
            return  null;
        }

       // girl.setAge(girl.getAge());
       // girl.setCupSize(girl.getCupSize());
       return girlRepository.save(girl);
    }
    /*
    通过ID查询
     */
    @GetMapping("/find/{id}")
    public Girl findById(@PathVariable("id") Integer id){

        return girlRepository.findById(id).orElse(null);
    }

    //更新
    @PostMapping(value = "/update/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                           @RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age) {
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);
    }

    //删除


}
